package cn.tp.ksoft.civil_driver.module.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import cn.tp.ksoft.civil_driver.DriverBaseActivity;
import cn.tp.ksoft.civil_driver.R;
import cn.tp.ksoft.civil_driver.bean.BrandVo;
import cn.tp.ksoft.civil_driver.bean.ResultBean;
import cn.tp.ksoft.civil_driver.request.DriverRequest;
import cn.tp.ksoft.civil_driver.utils.Constants;
import cn.tp.ksoft.civil_driver.view.simple.SnappingLinearLayoutManager;
import cn.tp.ksoft.civil_driver.view.widget.IndexView;
import cn.tp.ksoft.civil_driver.view.widget.PinnedRecycleView;

/**
 * Created by zhusong on 16/5/2.
 */
public class BrandActivity extends DriverBaseActivity {

    private IndexView mIndexView;
    private PinnedRecycleView mPinnedRv;
    private SparseArray<String> mSectionArr;
    private SparseArray<BrandVo> mContentArr;
    private SparseIntArray mSection2CountArr;
    private PinnedRecycleView.PinnedWrapAdapter mAdapter;
    private SnappingLinearLayoutManager mLayoutManager;
    private LinkedHashMap<String, ArrayList<String>> mData;

    private DriverRequest request;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.driver_activity_car_brand);
        updateTitle(getString(R.string.driver_car_brand));

        mIndexView = (IndexView) findViewById(R.id.index);
        mPinnedRv = (PinnedRecycleView) findViewById(R.id.rv);
        mLayoutManager = new SnappingLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mAdapter = mPinnedRv.setAdapter(new PinnedAdapter(), new BrandAdapter(), mSection2CountArr);
        mPinnedRv.setLayoutManager(mLayoutManager);
        mIndexView.setOnIndexClickListener(new IndexView.OnIndexClickListener() {
            @Override
            public void onIndexClick(IndexView.IndexItem item) {
                if (null == mSectionArr) {
                    return;
                }
                int index = 0;
                for (int i = 0, len = mSectionArr.size(); i < len; i++) {
                    if (mSectionArr.get(i).equals(item.getSectionName())) {
                        index = i;
                        break;
                    }
                }
                final int topIndex = mAdapter.getPositionForPinned(index);
                mPinnedRv.smoothScrollToPosition(topIndex);
            }
        });

        request = new DriverRequest(this, new WeakHandler(this));
        request.getBrandList();
    }

    private void updateData(List<BrandVo> list) {
        if (null == list) {
            return;
        }
        mSectionArr = new SparseArray<>();
        mContentArr = new SparseArray<>();
        mSection2CountArr = new SparseIntArray();
        mData = new LinkedHashMap<>();
        int sectionIndex = 0;
        int contentIndex = 0;
        for (int i = 0, len = list.size(); i < len; i++) {
            BrandVo vo = list.get(i);
            if (null == vo) {
                continue;
            }
            String name = vo.getName();
            String group = vo.getFirstletter();
            ArrayList<String> groupList;
            if (mData.containsKey(group)) {
                groupList = mData.get(group);
            } else {
                groupList = new ArrayList<String>();
                mData.put(group, groupList);
                mSectionArr.put(sectionIndex, group);
                sectionIndex++;
            }
            groupList.add(name);
            mContentArr.put(contentIndex, vo);
            contentIndex++;
        }

        Set<String> keySet = mData.keySet();
        String[] keys = new String[keySet.size()];
        keySet.toArray(keys);
        for (int i = 0, len = keys.length; i < len; i++) {
            mSection2CountArr.put(i, mData.get(keys[i]).size());
        }
        mAdapter.updateSectionCountArr(mSection2CountArr);
        mAdapter.notifyDataSetChanged();
    }

    class PinnedHolder extends RecyclerView.ViewHolder {
        TextView mTv;
        public PinnedHolder(View itemView) {
            super(itemView);
            mTv = (TextView) itemView.findViewById(R.id.tv);
        }
    }

    class PinnedAdapter extends RecyclerView.Adapter<PinnedHolder> {
        @Override
        public PinnedHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.driver_car_brand_head, viewGroup, false);
            return new PinnedHolder(view);
        }

        @Override
        public void onBindViewHolder(PinnedHolder viewHolder, int position) {
            viewHolder.mTv.setText(mSectionArr.get(position));
        }

        @Override
        public int getItemCount() {
            return null != mSectionArr ? mSectionArr.size() : 0;
        }
    }

    class BrandHolder extends RecyclerView.ViewHolder {
        ImageView mIv;
        TextView mTv;
        public BrandHolder(View itemView) {
            super(itemView);
            mIv = (ImageView) itemView.findViewById(R.id.iv_brand);
            mTv = (TextView) itemView.findViewById(R.id.tv_brand);
        }
    }
    class BrandAdapter extends RecyclerView.Adapter<BrandHolder> {
        @Override
        public BrandHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.driver_car_brand_item, viewGroup, false);
            return new BrandHolder(view);
        }

        @Override
        public void onBindViewHolder(BrandHolder brandHolder, int position) {
            final BrandVo vo = mContentArr.get(position);
            brandHolder.mTv.setText(vo.getName());
            brandHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent data = new Intent();
                    data.putExtra(Constants.KEY_BRAND, vo);
                    setResult(RESULT_OK, data);
                    finish();
                }
            });
        }

        @Override
        public int getItemCount() {
            return null != mContentArr ? mContentArr.size() : 0;
        }
    }

    @Override
    protected void handleSuccessResult(int what, @NonNull ResultBean result) {
        List<BrandVo> list = (List<BrandVo>) result.getData();
        updateData(list);
    }
}
